import numpy as np
import pandas as pd

"""
导出本次需要计算的数据
派单时间：考察全天的派单时间
"""

df_artisans = pd.read_csv('data/sample/artisans.csv')
df_orders = pd.read_csv('data/sample/sample_orders.csv')
# df_orders.sort_values(['start_hour', 'artisan_id'], inplace=True)
# df_orders.drop('Unnamed: 0', axis=1, inplace=True)
# df_orders = df_orders.reset_index(drop=True)
#
#
# def split_location(locations):
#     lon = None
#     lat = None
#     if locations is not None:
#         try:
#             lonlat = str(locations).split(',')
#             lon = round(float(lonlat[0]), 3)
#             lat = round(float(lonlat[1]), 3)
#         except:
#             print(str(locations))
#     return str(lon) + "," + str(lat)
#
#
# df_orders['point'] = df_orders['locations'].apply(split_location)
# df_orders['start'] = (df_orders['start_hour'] - 9) * 2
# df_orders['stop'] = df_orders['start'] + np.ceil(df_orders['prod_minute']/30)
# df_orders.to_csv('data/sample/sample_orders.csv')


def init_pop(se_artisan):
    """
    生成初始基因和种群
    :return:
    """
    artisan_id = se_artisan['id']
    orders = df_orders[df_orders['artisan_id'] == artisan_id]
    gens = [se_artisan.name]
    for half_hour in range(12 * 2):
        code = 0
        order = orders[(orders['start'] <= half_hour) & (orders['stop'] > half_hour)]
        if order is not None and len(order) > 0:
            code = order.index[0]
        gens.append(code)
    return gens


df_artisans['genes'] = df_artisans.apply(init_pop, axis=1)
df_artisans.drop('Unnamed: 0', axis=1, inplace=True)
df_artisans.to_csv('data/sample/artisans.csv')
